home *** CD-ROM | disk | FTP | other *** search
- Path: lyra.csx.cam.ac.uk!jkb
- From: jkb@mrc-lmb.cam.ac.uk (James Bonfield)
- Newsgroups: comp.std.c
- Subject: Re: Restrictions on qsort compare function?
- Date: 29 Mar 1996 12:43:37 GMT
- Organization: MRC Laboratory of Molecular Biology, Cambridge UK
- Message-ID: <4jgltp$f9l@lyra.csx.cam.ac.uk>
- References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com>
- NNTP-Posting-Host: alf2.mrc-lmb.cam.ac.uk
-
- I now agree that non antisymmetric or nontransitive sort comparison functions
- are indeed invalid. I can see how this can be construed from the ansi
- standard, but can also see how it's possible to construe otherwise. In short
- it doesn't really state such things explicitly. Anyway, that aside I thought I
- should reply to the last note about this.
-
- In article <4iqjar$2m9@usenet.pa.dec.com> diamond@tbj.dec.com (Norman Diamond) writes:
-
- >>such functions appear to make [one implementation's] qsort() function
- >>underflow the passed array.
- >
- >This should not happen.
-
- Well, that depends on whether or not you class qsorts behaviour as undefined
- for such functions.
-
- >>#define NUM_ELE 10
- >>int main() {
- >> int i;
- >> int crashme; /* removing this line fixes things! */
- >
- >This makes me suspicious that your test program is not exactly what you
- >posted, and your test program has a bug in some other part of its code
- >that already yielded undefined behavior.
-
- Actually it's true - it does cause it to go wrong. I've got an example now
- that used explicitly defined numbers to cause a crash. With the crashme line
- in the program dies. Without it the program modifies memory not within the
- boundaries of qsort. My program is:
-
- #include <stdio.h>
- #include <stdlib.h>
-
- static int sort_func(const void *pa, const void *pb)
- {
- const int *a = (int *)pa;
- const int *b = (int *)pb;
-
- return *a > *b;
- }
-
- #define NUM_ELE 10
- int main() {
- int i;
- int crashme; /* removing this line fixes things! */
- int sortme[NUM_ELE] = {148, 104, 126, 74, 108, 131, 129, 131, 125, 77};
-
- for (i=0; i<NUM_ELE; i++) printf("%d ", sortme[i]);
- putchar('\n');
-
- qsort((void *)(sortme+1), NUM_ELE-2, sizeof(int), sort_func);
-
- for (i=0; i<NUM_ELE; i++) printf("%d ", sortme[i]);
- putchar('\n');
-
- return 0;
- }
-
- >b < c and c < a). As for whether qsort() is required to contend with
- >such nonsense, it "probably" isn't.
-
- Agreed. Hence the above discoveries are most likely entirely within the realm
- of acceptability.
-
- Perhaps the most interesting point to come out of this is the inefficiency of
- some qsort algorithms. Sorting 100,000 elements (with a "consistent" sort
- comparison function) gave the following approximations (ran several times with
- random input - of course these results may just reflect the random number
- generator woes!):
-
- OSF/1 V3.0 ~72K
- Linux (gnu lib) ~153K
- Irix 5.3 ~170K
- Solaris 5.3 ~305K
-
- Thanks for all the suggestions everyone has made.
- Cheers,
-
- James
- --
- James Bonfield (jkb@mrc-lmb.cam.ac.uk) Tel: 01223 402499 Fax: 01223 412282
- Medical Research Council - Laboratory of Molecular Biology,
- Hills Road, Cambridge, CB2 2QH, England.
-